#
# Do not alter this file, create a copy with 
# a different name and modify that. Keep the extension .py
# otherwise it cannot be imported.
#
# Web processing configuration module set up to 
# be run after the dataconf.py config module to
# serve the data created during that 
# 
# In this file capitalized attributes must be present (GeneTrack
# will check for their presence).
#
# All attributes may be written out as text but we use
# simple python functions to be able to generate them.
# This makes changing the attributes (such as labels) 
# a lot less error prone.
#
import atlas, sys
import functools

#
# The title displayed on the index page.
#
TITLE = "Genetrack Browser"

#
# Directory setup, a string containing the 
# full path to a directory. 
#
home = atlas.ENV.HOME_DIR 

#
# Typically these directories do not need to be altered.
# Files placed in the DOWNLOAD_DIR will be downloadable
# via the webserver.
#
TEMPLATE_DIR = home + "/html"
SESSION_DIR  = home + "/html/session"
STATIC_DIR   = home + "/html/static"
DOWNLOAD_DIR = home + "/html/static/download"
IMAGE_DIR    = home + "/html/static/img"
STATIC_URL   = "/static"

#
# Database setup. The same parameter substitution as in dataconf.py
#
organism = "yeast"
HDF_DATABASE = "%s/db/%s-data.hdf" % (home, organism)
SQL_URI      = "sqlite:///%s/db/%s-data.sqlite" % (home, organism)

#
# Generating a chromosome list widget based on data/
#
# The value of the CHROM_FIELDS could be hardcoded as a list of tuples
# but it is more reliable to dynamically generate it from a HDF file
#
try:
    from atlas import hdf
    db = hdf.hdf_open( HDF_DATABASE, mode='r' )
    data = hdf.GroupData(db=db, name='H2AZ')
    chromosomes = data.labels
    db.close()
except Exception, exc:
    atlas.error("unable to read chromosomes %s" % exc )
    sys.exit()
    
#
# Generate the chromosome dropdown menu. The first element of the tuple os
# the key the second is the visible text in the menu (kept the same in this case).
#
CHROM_FIELDS = [ (x, x) for x in chromosomes ]

#
# These parameters are used to generate the zoom level dropdown menu.
#
LEVELS      = [ 50, 100, 250, 500, 1000, 2500, 5000, 10000, 20000, 50000, 100000, 200000 ]
LEVELS      = map(str, LEVELS)
humanized   = map(atlas.commify, LEVELS)

#
# Format the displayed levels with commas 100000 -> 100,000
#
ZOOM_LEVELS = [ (x, y) for x,y in zip(LEVELS, humanized) ]

#
# Generates the dropdown menu for the plot.
# The first element is the key the second the visible menu item.
# Keys must be present in the  PLOT_MAPPER that will actually select
# the plotting function that is associated with a key
#
PLOT_CHOICES = [ 
    ('composite', 'Composite'), ('twostrand', 'Two Stranded'),
]

#
# Importing the main plotbuilder
#
from mod454.trackbuilder import build_tracks

#
# A plotting function has an interface with a two parameters:
#
# param -> represents the incoming parameters submitted by the user
# conf  -> represents a configuration module 
#
#
# Customize the plotbuilder via partial application to generate 
# function with the interface above
#

# 
# The function for twostrand tracks
#
h2az_twostrand_tracks = functools.partial ( build_tracks, 
        data_label = 'H2AZ', 
        fit_label  = 'H2AZ-SIGMA-20', 
        pred_label = 'PRED-H2AZ-SIGMA-20', 
        strand     = 'twostrand' )

#
# The function for composite tracks
#
h2az_composite_tracks = functools.partial ( build_tracks, 
        data_label = 'H2AZ', 
        fit_label  = 'H2AZ-SIGMA-20', 
        pred_label = 'PRED-H2AZ-SIGMA-20', 
        strand     = 'composite' )

#
# The plotmapper connects the plot choice to a template and a plotting function
#
PLOT_MAPPER = {
    'composite': ( 'index.html', h2az_composite_tracks ),
    'twostrand': ( 'index.html', h2az_twostrand_tracks ),
}

#
# This is a sanity check to catch errors early on.
#
for key, value in PLOT_CHOICES:
    assert key in PLOT_MAPPER, "Key '%s' not found in PLOT_MAPPER" % key

#
# Some socket related configuration.
#
SOCKET_PORT = 8080
SOCKET_HOST = "localhost"
THREAD_POOL = 10
